﻿using MCPlatform.MotionLib.Base.Environment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.Test.ParsingDLL
{
    public class ParsingProbeConfigTxt
    {
        public StrMTPA16DefaultConfidData GetData()
        {
            // 文件路径
            // string filePath = "C:\\Users\\LegionP\\source\\repos\\CDllParsing\\CDllParsing\\bin\\Debug\\net6.0-windows\\MTPA16L2_0ConfigFile.txt";

            string filePath = Paths.TextConfigPaths;

            // 创建结构体实例
            StrMTPA16DefaultConfidData data = new StrMTPA16DefaultConfidData();
            // 读取文本文件
            using (StreamReader reader = new StreamReader(filePath))
            {


                // 逐行读取文件内容
                string? line;
                while ((line = reader.ReadLine()) != null)
                {
                    // 解析每一行数据
                    ParseLine(line, ref data);
                }

                // 打印结构体中的数据（此处仅为演示）
                // PrintData(data);
            }

            return data;
        }


        public Dictionary<string,string>? GetBasicConfig(string? path = null)
        {
            string filePath = Paths.NumberProbeHeighTempeConfigPaths;

            if (path != null)
            {
                filePath = path;
            }

            if (!File.Exists(filePath))
            {               
                return null;
            }



            Dictionary<string, string> configDict = new Dictionary<string, string>();

           
            // 读取文本文件的每一行并解析配置
            string[] lines = File.ReadAllLines(filePath);

            foreach (string line in lines)
            {
                // 切分每一行的键值对
                string[] parts = line.Split('：'); // 根据具体文档格式可能需要调整分隔符
                if (parts.Length < 2)
                {
                    parts = line.Split(':');
                }
                if (parts.Length == 2)
                {
                    string key = parts[0].Trim();

                    if (key.Contains("scale"))
                    {
                        key = "Vt_scale";
                        configDict[key] = parts[1].Trim() + "mV";
                    }
                    else if (key.Contains("gain"))
                    {
                        key = "PGA_Gain";
                        configDict[key] = "x" + parts[1].Trim();
                    }
                    else if (key.Contains("osr"))
                    {
                        key = "OSR";
                        configDict[key] =  parts[1].Trim();
                    }
                    else if (key.Contains("辐射"))
                    {
                        key = "RadiationCoefficient";
                        configDict[key] =  parts[1].Trim();
                    }





                }
               
                if (configDict.Count>= 4 )
                {
                    
                    break;
                }
            }

            return configDict;
        }


        public Dictionary<string, string>? GetCalibrateParameterConfig(string? path = null)
        {
            string filePath = Paths.NumberProbeHeighTempeConfigPaths;

            if (path != null)
            {
                filePath = path;
            }

            if (!File.Exists(filePath))
            {
                return null;
            }



            Dictionary<string, string> configDict = new Dictionary<string, string>();


            // 读取文本文件的每一行并解析配置
            string[] lines = File.ReadAllLines(filePath);

            foreach (string line in lines)
            {

                if (line.Contains("次数") || line.Contains("电压值") || line.Contains("校准"))
                {
                    // 切分每一行的键值对
                    string[] parts = line.Split('：'); // 根据具体文档格式可能需要调整分隔符
                    if (parts.Length < 2)
                    {
                        parts = line.Split(':');
                    }
                   
                    if (parts.Length == 2)
                    {
                        string key = parts[0].Trim();
                       

                        if (key.Contains("校准环境采集次数"))
                        {
                            key = "CalibraEnvCollectFrequency";
                           
                        }
                        else if (key.Contains("校准环境温度"))
                        {
                            key = "CalibraEnvTemperature";

                        }
                        else if (key.Contains("offset电压值"))
                        {
                            key = "OffsetVoltageValue";
                           
                        }
                        else if (key.Contains("offset采集次数"))
                        {
                            key = "OffsetCollectFrequency";
                           
                        }
                        else if (key.Contains("gain电压值"))
                        {
                            key = "GainVoltageValue";
                           
                        }
                        else if (key.Contains("gain采集次数"))
                        {
                            key = "GainCollectFrequency";

                        }

                        // 将键值对存入字典
                        configDict[key] = parts[1].Trim();
                    }
                   
                }
                

              
            }

            return configDict;
        }


        public Dictionary<int, double>? GetVtTable(string? path = null)
        {
            string filePath = Paths.NumberProbeHeighTempeConfigPaths;

            if (path != null)
            {
                filePath = path;
            }

            if (!File.Exists(filePath))
            {
                return null;
            }
            Dictionary<int, double> data = new Dictionary<int, double>();         

           
            // 读取文本文件的每一行并解析配置
            string[] lines = File.ReadAllLines(filePath);

            string vt = string.Empty;

            foreach (string line in lines)
            {

                if (vt != string.Empty)
                {

                    string[] parts = line.Split('\t');
                    if (parts.Length == 2)
                    {
                        string key = parts[0].Trim();
                        string value = parts[1].Trim();
                        int.TryParse(key, out int keyUint);
                        double.TryParse(value, out double valueDouble);

                        // 将键值对添加到Dictionary中
                        data[keyUint] = valueDouble;
                    }

                }


                if (line.Contains("VT表"))
                {
                    vt = line;
                }


            }

            return data;

           
        }


        // 解析每一行数据并存储到结构体中
        private static void ParseLine(string line, ref StrMTPA16DefaultConfidData data)
        {
            string[] parts = line.Split(':');
            if (parts.Length != 2)
                return;

            string dataName = parts[0].Trim();
            string value = parts[1].Trim();

            // 提取前缀和索引（如果有的话）
            // string prefix = new String(dataName.TakeWhile(Char.IsLetter).ToArray());
            if (!dataName.Contains("."))
            {
                return;
            }
            string prefix = dataName.Split('.')[1];


            // 将前缀映射到结构体中相应的数组
            switch (prefix)
            {
                case "CL":
                    data.fCL = float.Parse(value);
                    break;
                case "TVT1A0":
                    data.fTVT1[0] = float.Parse(value);
                    break;
                case "TVT1A1":
                    data.fTVT1[1] = float.Parse(value);
                    break;
                case "TVT1A2":
                    data.fTVT1[2] = float.Parse(value);
                    break;
                case "TVT1A3":
                    data.fTVT1[3] = float.Parse(value);
                    break;
                case "TVT2B0":
                    data.fTVT2[0] = float.Parse(value);
                    break;
                case "TVT2B1":
                    data.fTVT2[1] = float.Parse(value);
                    break;
                case "TVT2B2":
                    data.fTVT2[2] = float.Parse(value);
                    break;
                case "TVT2B3":
                    data.fTVT2[3] = float.Parse(value);
                    break;
                case "VTI0":
                    data.fVTI[0] = float.Parse(value);
                    break;
                case "VTI1":
                    data.fVTI[1] = float.Parse(value);
                    break;
                case "VTI2":
                    data.fVTI[2] = float.Parse(value);
                    break;
                case "VTI3":
                    data.fVTI[3] = float.Parse(value);
                    break;
                case "VTI4":
                    data.fVTI[4] = float.Parse(value);
                    break;
                case "VTT1A0":
                    data.fVTT1[0] = float.Parse(value);
                    break;
                case "VTT1A1":
                    data.fVTT1[1] = float.Parse(value);
                    break;
                case "VTT1A2":
                    data.fVTT1[2] = float.Parse(value);
                    break;
                case "VTT1A3":
                    data.fVTT1[3] = float.Parse(value);
                    break;
                case "VTT2B0":
                    data.fVTT2[0] = float.Parse(value);
                    break;
                case "VTT2B1":
                    data.fVTT2[1] = float.Parse(value);
                    break;
                case "VTT2B2":
                    data.fVTT2[2] = float.Parse(value);
                    break;
                case "VTT2B3":
                    data.fVTT2[3] = float.Parse(value);
                    break;
                case "VTT3C0":
                    data.fVTT3[0] = float.Parse(value);
                    break;
                case "VTT3C1":
                    data.fVTT3[1] = float.Parse(value);
                    break;
                case "VTT3C2":
                    data.fVTT3[2] = float.Parse(value);
                    break;
                case "VTT3C3":
                    data.fVTT3[3] = float.Parse(value);
                    break;
                case "VTT4D0":
                    data.fVTT4[0] = float.Parse(value);
                    break;
                case "VTT4D1":
                    data.fVTT4[1] = float.Parse(value);
                    break;
                case "VTT4D2":
                    data.fVTT4[2] = float.Parse(value);
                    break;
                case "VTT4D3":
                    data.fVTT4[3] = float.Parse(value);
                    break;
                case "DPixADDR0":
                    data.u16DPixAddr[0] = ushort.Parse(value);
                    break;
                case "DPixADDR1":
                    data.u16DPixAddr[1] = ushort.Parse(value);
                    break;
                case "DPixADDR2":
                    data.u16DPixAddr[3] = ushort.Parse(value);
                    break;
                case "DPixADDR3":
                    data.u16DPixAddr[3] = ushort.Parse(value);
                    break;
                case "DPixADDR4":
                    data.u16DPixAddr[4] = ushort.Parse(value);
                    break;
                case "DPixADDR5":
                    data.u16DPixAddr[5] = ushort.Parse(value);
                    break;
                case "DPixADDR6":
                    data.u16DPixAddr[6] = ushort.Parse(value);
                    break;
                case "DPixADDR7":
                    data.u16DPixAddr[7] = ushort.Parse(value);
                    break;
                case "DPixADDR8":
                    data.u16DPixAddr[8] = ushort.Parse(value);
                    break;
                case "DPixADDR9":
                    data.u16DPixAddr[9] = ushort.Parse(value);
                    break;
                case "DPixADDR10":
                    data.u16DPixAddr[10] = ushort.Parse(value);
                    break;
                case "DPixADDR11":
                    data.u16DPixAddr[11] = ushort.Parse(value);
                    break;
                case "DPixADDR12":
                    data.u16DPixAddr[12] = ushort.Parse(value);
                    break;

                case "DPixADDR13":
                    data.u16DPixAddr[13] = ushort.Parse(value);
                    break;
                case "DPixADDR14":
                    data.u16DPixAddr[14] = ushort.Parse(value);
                    break;
                case "DPixADDR15":
                    data.u16DPixAddr[15] = ushort.Parse(value);
                    break;
                case "DPixADDR16":
                    data.u16DPixAddr[16] = ushort.Parse(value);
                    break;
                case "DPixADDR17":
                    data.u16DPixAddr[17] = ushort.Parse(value);
                    break;
                case "DPixADDR18":
                    data.u16DPixAddr[18] = ushort.Parse(value);
                    break;
                case "DPixADDR19":
                    data.u16DPixAddr[19] = ushort.Parse(value);
                    break;
                case "DPixADDR20":
                    data.u16DPixAddr[20] = ushort.Parse(value);
                    break;
                case "DPixADDR21":
                    data.u16DPixAddr[21] = ushort.Parse(value);
                    break;
                case "DPixADDR22":
                    data.u16DPixAddr[22] = ushort.Parse(value);
                    break;
                case "DPixMask0":
                    data.u8DPixMask[0] = byte.Parse(value);
                    break;
                case "DPixMask1":
                    data.u8DPixMask[1] = byte.Parse(value);
                    break;
                case "DPixMask2":
                    data.u8DPixMask[2] = byte.Parse(value);
                    break;
                case "DPixMask3":
                    data.u8DPixMask[3] = byte.Parse(value);
                    break;
                case "DPixMask4":
                    data.u8DPixMask[4] = byte.Parse(value);
                    break;
                case "DPixMask5":
                    data.u8DPixMask[5] = byte.Parse(value);
                    break;
                case "DPixMask6":
                    data.u8DPixMask[6] = byte.Parse(value);
                    break;
                case "DPixMask7":
                    data.u8DPixMask[7] = byte.Parse(value);
                    break;
                case "DPixMask8":
                    data.u8DPixMask[8] = byte.Parse(value);
                    break;
                case "DPixMask9":
                    data.u8DPixMask[9] = byte.Parse(value);
                    break;
                case "DPixMask10":
                    data.u8DPixMask[10] = byte.Parse(value);
                    break;
                case "DPixMask11":
                    data.u8DPixMask[11] = byte.Parse(value);
                    break;
                case "DPixMask12":
                    data.u8DPixMask[12] = byte.Parse(value);
                    break;
                case "DPixMask13":
                    data.u8DPixMask[13] = byte.Parse(value);
                    break;
                case "DPixMask14":
                    data.u8DPixMask[14] = byte.Parse(value);
                    break;
                case "DPixMask15":
                    data.u8DPixMask[15] = byte.Parse(value);
                    break;
                case "DPixMask16":
                    data.u8DPixMask[16] = byte.Parse(value);
                    break;
                case "DPixMask17":
                    data.u8DPixMask[17] = byte.Parse(value);
                    break;
                case "DPixMask18":
                    data.u8DPixMask[18] = byte.Parse(value);
                    break;
                case "DPixMask19":
                    data.u8DPixMask[19] = byte.Parse(value);
                    break;
                case "DPixMask20":
                    data.u8DPixMask[20] = byte.Parse(value);
                    break;
                case "DPixMask21":
                    data.u8DPixMask[21] = byte.Parse(value);
                    break;
                case "DPixMask22":
                    data.u8DPixMask[22] = byte.Parse(value);
                    break;
                case "DPixMask23":
                    data.u8DPixMask[23] = byte.Parse(value);
                    break;
                case "DPixNum":

                    data.u8DPixNum = byte.Parse(value);
                    break;

                case "Reg1":
                    data.u8Reg[0] = Convert.ToByte(value, 16);
                    break;
                case "Reg2":
                    data.u8Reg[1] = Convert.ToByte(value, 16);
                    break;
                case "Reg3":
                    data.u8Reg[2] = Convert.ToByte(value, 16);
                    break;
                case "Reg04":
                    data.u8Reg[3] = Convert.ToByte(value, 16);
                    break;
                case "Reg5":
                    data.u8Reg[4] = Convert.ToByte(value, 16);
                    break;
                case "Reg6":
                    data.u8Reg[5] = Convert.ToByte(value, 16);
                    break;
                case "Reg7":
                    data.u8Reg[6] = Convert.ToByte(value, 16);
                    break;
                case "Reg8":
                    data.u8Reg[7] = Convert.ToByte(value, 16);
                    break;
                case "Reg9":
                    data.u8Reg[8] = Convert.ToByte(value, 16);
                    break;
                case "Reg10":
                    data.u8Reg[9] = Convert.ToByte(value, 16);
                    break;
                case "Reg11":
                    data.u8Reg[10] = Convert.ToByte(value, 16);
                    break;
                case "epsilon":
                    data.u8eplison = byte.Parse(value);
                    break;
                case "DeviceID":
                    data.u8DeviceID = Encoding.UTF8.GetBytes(value);
                    // byte.TryParse(value, out data.u8DeviceID[3]);
                    break;
                case "SN":
                    data.u8SN = Encoding.UTF8.GetBytes(value);
                    //  byte.TryParse(value, out data.u8SN[7]);
                    break;
                case "ProdDesc":
                    data.u8ProdDesc = Encoding.UTF8.GetBytes(value);
                    break;
                case "GlobalCoe1":
                    data.fGlobalCoe1 = float.Parse(value);
                    break;
                case "GlobalCoe2":
                    data.fGlobalCoe2 = float.Parse(value);
                    break;
                case "DataFlag":
                    data.u16DataFlag = Convert.ToUInt16(value, 16);
                    break;
                // 根据需要添加其他前缀的情况
                default:
                    break;
            }

            // 根据需要添加其他前缀的情况
        }
    }
}
